package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	s := "啊切home"       // 6个字符
	fmt.Println(len(s)) // 10   (26-18)/4=2 一个中文占三个字节
	for _, b := range []byte(s) {
		fmt.Printf("%X ", b) // utf-8  E5 95 8A E5 88 87 68 6F 6D 65
	}
	fmt.Println()

	for i, ch := range s { // ch is a rune(char) : 四字节整数
		fmt.Printf("(%d %X) ", i, ch) // (0 554A) (3 5207) (6 68) (7 6F) (8 6D) (9 65)
	}
	fmt.Println()

	fmt.Println("Rune count:",
		utf8.RuneCountInString(s)) // 6 个字符

	bytes := []byte(s)
	for len(bytes) > 0 {
		ch, size := utf8.DecodeRune(bytes)
		bytes = bytes[size:]
		fmt.Printf("%c ", ch) // 啊 切 h o m e
	}
	fmt.Println()

	for i, ch := range []rune(s) {
		fmt.Printf("(%d %c) ", i, ch) //  (0 啊) (1 切) (2 h) (3 o) (4 m) (5 e)
	}
	fmt.Println()
}
